package be.kdg.componenten.chatters;

import be.kdg.componenten.client.ChatClientFrame;

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.Remote;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.net.MalformedURLException;

/**
 * Tom Verelst
 * Wim Goeman
 */
public class ChatterImpl extends UnicastRemoteObject implements Chatter {

    private String name;
    private Chatters chatters;
    private TextReceiver receiver;

    public ChatterImpl(String name) throws RemoteException{
        this.name = name;
        this.chatters = new ChattersImpl();
        receiver = new ChatClientFrame(name, this);
    }

    public String getName() throws RemoteException{
        return name;
    }

    public void setName(String name) throws RemoteException {
        this.name = name;
    }

    public void register(Chatter chatter) throws RemoteException {
        chatters.add(chatter);
        receiver.receive("<"+chatter.getName()+"> has joined the room.");
        receiver.updateChatters(chatters);
    }

    public void unregister(Chatter chatter) throws RemoteException{
        if(chatter.getName().equals(this.name)){
            for(Chatter other : chatters.getChatters()){
                if(!other.getName().equals(this.name)){
                    other.unregister(this);
                }
            }
        }
        chatters.remove(chatter); 
        receiver.receive("<"+chatter.getName()+"> has left the room.");
        receiver.updateChatters(chatters);
    }

    public void send(String text) throws RemoteException{
        chatters.receive(name, text);    
    }

    public void receive(String name, String text) throws RemoteException{
        System.out.println("Received a message");
        receiver.receive(name + ": " + text);
    }

    public Chatters getChatters() throws RemoteException {
        return chatters;
    }

    public void run() throws MalformedURLException, NotBoundException, RemoteException {
        int portNumber = 1099;
        Registry registry;
        Chatter chatter;

        try{ // Try to create new registry
            System.out.println("try");
            registry = LocateRegistry.createRegistry(portNumber);
            System.out.println("created Registry");
            chatter = this;
            registry.rebind("chatter", chatter);
        }catch(ExportException ex){ // If registry already exists, get existing one
            System.out.println("catched");
            registry = LocateRegistry.getRegistry(portNumber);
            System.out.println("got registry");
        }

        // Check if chatter is already in registry
        String[] list = registry.list();
        Boolean bestaat = false;
        for(int i = 0; i < list.length; i++){
            if(list[i].equals("chatter")){
                bestaat = true;
            }
        }

        if(bestaat){
            chatter = (Chatter) registry.lookup("chatter");
            this.chatters = chatter.getChatters();
        }
        else {
            chatter = this;
            registry.rebind("chatter", this);
        }
        if(chatters.contains(name)){
            System.exit(1);
        }else{
            for(Chatter other : chatters.getChatters()){
                other.register(this);
            }
            chatters.add(this);
            receiver.updateChatters(chatters);
        }
    }



}
